﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cbmobile.BLC;
using cbmobile.DAC;
using PQT.API;
using PQT.API.File;
using PQT.Common;
using PQT.Controls;
using Telerik.WebControls;
using PQT.API.DataDefine.Sys;

namespace cbmobile.UserControl
{
    public partial class SanPham_Entry : CommonUserControl
    {
        BLC_HangSanXuat blc_HangSX = new BLC_HangSanXuat();
        BLC_SanPham blc_SanPham = new BLC_SanPham();
        #region upload
        private string uploadimg(FileUpload fileImage)
        {
            //try
            //{
                SanPham entP = blc_SanPham.Get_SanPham_byId(this.Ma_SP);
                string filePath = fileImage.FileName;
                if (entP != null)
                {
                    if (filePath !="")
                    {
                        fileImage.SaveAs(MapPath("~/ImagesSP/" + filePath));

                        return filePath;
                    }
                    else
                    {
                        filePath = entP.HinhAnh;
                        return filePath;
                    }
                    

                    //fileImage.SaveAs(MapPath("~/ImagesSP/" + filePath));

                    
                }
                else
                {
                    

                    fileImage.SaveAs(MapPath("~/ImagesSP/" + filePath));

                    return filePath;
                }


                

            //}
            //catch (System.Exception ex)
            //{
            //    return 0;
            //}

        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["action"] != null)
                {
                    switch (Request.QueryString["action"].ToLower())
                    {
                        case "addnew": frmBlogCate.ChangeMode(FormViewMode.Insert);
                            break;
                    }
                }
            }
        }

        protected bool CheckStatus(object ck_Stt)
        {
            if (ck_Stt != null)
            {
                int ck = Convert.ToInt32(!string.IsNullOrEmpty(ck_Stt.ToString()) ? ck_Stt.ToString() : "0");
                //if (ck_Stt.ToString() != null && ck_Stt.ToString() != string.Empty)
                //{ ck_Stt.ToString}
                return ck == 1 ? true : false;
            }
            return false;
        }

        private DropDownList findcontrol_DropList(string findcontrol)
        {
            return frmBlogCate.FindControl(findcontrol) as DropDownList;
        }


        private void BindControlField()
        {
            SanPham entP = blc_SanPham.Get_SanPham_byId(this.Ma_SP);
            DropDownList ddlStatus = frmBlogCate.FindControl("ddlStatus") as DropDownList;
            if (ddlStatus != null)
            {
                ddlStatus.Items.Clear();

                ddlStatus.Items.Add(new ListItem(CommonStatus.Inactive.ToString(), ((int)CommonStatus.Inactive).ToString()));
                ddlStatus.Items.Add(new ListItem(CommonStatus.Active.ToString(), ((int)CommonStatus.Active).ToString()));

                if (entP != null)
                {
                    ddlStatus.SelectedValue = entP.TinhTrang.ToString();
                }
            }

            //Label lblHinhAnh = frmBlogCate.FindControl("lblHinhAnh") as Label;
            //if (lblHinhAnh != null && entP != null)
            //{
                
            //    //imgProduct.ImageUrl = GetImagePath(Utilis.TryParseLong(entP.HinhAnh));
            //    //lblHinhAnh.Text = GetImageName(Utilis.TryParseLong(entP.HinhAnh));
            //}
        }

        //private string GetImagePath(long p_fileID)
        //{
        //    FileManager fileMng = new FileManager();
        //    CommonFileEntity fileEnt = fileMng.RowCommonFile(p_fileID);
        //    if (fileEnt != null)
        //        return "/" + System.Configuration.ConfigurationManager.AppSettings["ProductImagePath"].Replace("\\", "/") + fileEnt.ServerFileName;
        //    return string.Empty;
        //}

        //private string GetImageName(long p_fileID)
        //{
        //    FileManager fileMng = new FileManager();
        //    CommonFileEntity fileEnt = fileMng.RowCommonFile(p_fileID);

        //    if (fileEnt != null)
        //    {
        //        return fileEnt.RealFileName.ToString();
        //    }
        //    return string.Empty;
        //}

        protected string StatusName(object StatusID)
        {
            return StatusID != null ? ((CommonStatus)StatusID).ToString() : string.Empty;
        }

        protected int Ma_SP
        {
            get
            {
                return Utility.ValidateParam("Ma_SP", 0);
            }
        }

        protected void frmBlogCate_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {

        }

        protected void frmBlogCate_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            Response.Redirect(this.ListLink);
        }

        protected void frmBlogCate_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            Response.Redirect(this.ListLink);
        }

        protected void frmBlogCate_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            
        }

        protected void ObjectDataSource1_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            DropDownList ddlStatus = frmBlogCate.FindControl("ddlStatus") as DropDownList;
            e.InputParameters["TinhTrang"] = ddlStatus.SelectedValue;
            FileUpload fileImage = frmBlogCate.FindControl("fileImage") as FileUpload;
            e.InputParameters["HinhAnh"] = uploadimg(fileImage);
        }

        protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            DropDownList ddlStatus = frmBlogCate.FindControl("ddlStatus") as DropDownList;
            e.InputParameters["TinhTrang"] = ddlStatus.SelectedValue;
            FileUpload fileImage = frmBlogCate.FindControl("fileImage") as FileUpload;
            e.InputParameters["HinhAnh"] = uploadimg(fileImage);
        }

        protected void frmBlogCate_DataBound(object sender, EventArgs e)
        {
            BindControlField();
        }
    }
}